package com.cn.common.utils.date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class DateUtil extends DateUtils {
    public static String YYYY = "yyyy";
    public static String YYYY_MM = "yyyy-MM";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String HH_MM_SS = "HH:mm:ss";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String YYYYMMDDHHMM = "yyyyMMddHHmm";
    public static final String DATE_PATTERN_YYYYMMDD = "yyyy/MM/dd";
    public static final String WEEK_FLAG = "EEEE";
    private static String[] parsePatterns = new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
    public static final String MONDAY = "星期一";
    public static final String TUESDAY = "星期二";
    public static final String WEDNESDAY = "星期三";
    public static final String THURSDAY = "星期四";
    public static final String FRIDAY = "星期五";
    public static final String SATURDAY = "星期六";
    public static final String SUNDAY = "星期日";

    public DateUtil() {
    }

    public static SimpleDateFormat getYyyyMmDdHhMmSs() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    public static SimpleDateFormat getDEFAULT_FORMATTER() {
        return new SimpleDateFormat("yyyy-MM-dd");
    }

    public static String getDate(Date date, String dateFormat) {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        return format.format(date);
    }

    public static Date getDate(String date, String fromFormat) {
        try {
            SimpleDateFormat fromSimpleDateFormat = new SimpleDateFormat(fromFormat);
            return fromSimpleDateFormat.parse(date);
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static Date getDate(String date) {
        SimpleDateFormat sdf = getYyyyMmDdHhMmSs();

        try {
            return sdf.parse(date);
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static boolean isValid(String dateStr, String fromFormat) {
        if (!StringUtils.isEmpty(dateStr) && !StringUtils.isEmpty(fromFormat)) {
            DateTimeFormatter ldt = DateTimeFormatter.ofPattern(fromFormat.replace("y", "u")).withResolverStyle(ResolverStyle.STRICT);

            try {
                return LocalDate.parse(dateStr, ldt) != null;
            } catch (Exception var4) {
                return false;
            }
        } else {
            return false;
        }
    }

    public static boolean isThisTime(Date time, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String param = sdf.format(time);
        String now = sdf.format(new Date());
        return param.equals(now);
    }

    public static Date startOfDay(Date date) throws ParseException {
        return (new SimpleDateFormat("yyyy-MM-dd")).parse((new SimpleDateFormat("yyyy-MM-dd")).format(date));
    }

    public static Date anotherDay(Date date, int i) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(5, i);
        Date result = cal.getTime();
        return result;
    }

    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = getDEFAULT_FORMATTER();
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static String dateToStr(Date dateDate) {
        if (dateDate == null) {
            return null;
        } else {
            SimpleDateFormat formatter = getDEFAULT_FORMATTER();
            return formatter.format(dateDate);
        }
    }

    public static String dateTimeToStr(Date dateDate) {
        SimpleDateFormat formatter = getYyyyMmDdHhMmSs();
        return formatter.format(dateDate);
    }

    public static String dateTimeToStr() {
        Date dateDate = new Date();
        SimpleDateFormat formatter = getYyyyMmDdHhMmSs();
        return formatter.format(dateDate);
    }

    public static final String getCurrentDateToString() {
        Calendar calendar = Calendar.getInstance();
        int i = calendar.get(1);
        int j = calendar.get(2) + 1;
        int k = calendar.get(5);
        return String.valueOf(i + (j >= 10 ? "" + j : "0" + j) + (k >= 10 ? "" + k : "0" + k));
    }

    public static Date stepYear(Date sourceDate, int year) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(1, year);
        return c.getTime();
    }

    public static Date stepMonth(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(2, month);
        return c.getTime();
    }

    public static String getCurrentDate() {
        return getDate(new Date(), "yyyy-MM-dd HH:mm:ss").substring(0, 10);
    }

    public static Date transferDate(Date date) {
        return date != null ? getDate(getCurrentDate() + " " + getDate(date, "yyyy-MM-dd HH:mm:ss").substring(11, 19)) : null;
    }

    public static Date transferDateTime(String date) {
        return date != null ? getDate(date + " " + getDate(new Date(), "yyyy-MM-dd HH:mm:ss").substring(11, 19)) : null;
    }

    public static Date dateTimeToTimeEndZero(Date dateTime) {
        String dateTimeString = getDate(dateTime, "yyyy-MM-dd HH:mm:ss");
        return getDate(dateTimeString.substring(0, 10) + " 00:00:00");
    }

    public static Date dateTimeEndWidth(Date dateTime) {
        String dateTimeString = getDate(dateTime, "yyyy-MM-dd HH:mm:ss");
        return getDate(dateTimeString.substring(0, 10) + " 23:59:59");
    }

    public static Date dateTimeYearEndWidth(Date dateTime) {
        String dateTimeString = getDate(dateTime, "yyyy-MM-dd HH:mm:ss");
        return getDate(dateTimeString.substring(0, 4) + "-12-31 23:59:59");
    }

    public static Date getNowDate() {
        return new Date();
    }

    public static String getDate() {
        return dateTimeNow("yyyy-MM-dd");
    }

    public static final String getTime() {
        return dateTimeNow("yyyy-MM-dd HH:mm:ss");
    }

    public static final String dateTimeNow() {
        return dateTimeNow("yyyyMMddHHmmss");
    }

    public static final String dateTimeNow(String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(Date date) {
        return parseDateToStr("yyyy-MM-dd", date);
    }

    public static final String parseDateToStr(String format, Date date) {
        return (new SimpleDateFormat(format)).format(date);
    }

    public static final Date dateTime(String format, String ts) {
        try {
            return (new SimpleDateFormat(format)).parse(ts);
        } catch (ParseException var3) {
            throw new RuntimeException(var3);
        }
    }

    public static final String datePath(String format) {
        Date now = new Date();
        return DateFormatUtils.format(now, format);
    }

    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        } else {
            try {
                return parseDate(str.toString(), parsePatterns);
            } catch (ParseException var2) {
                return null;
            }
        }
    }

    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 86400000L;
        long nh = 3600000L;
        long nm = 60000L;
        long diff = endDate.getTime() - nowDate.getTime();
        long day = diff / nd;
        long hour = diff % nd / nh;
        long min = diff % nd % nh / nm;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static int differentDaysByMillisecond(Date date1, Date date2) {
        return Math.abs((int)((date2.getTime() - date1.getTime()) / 86400000L));
    }

    public static int differentHourByMillisecond(Date date1, Date date2) {
        return Math.abs((int)((date2.getTime() - date1.getTime()) / 3600000L));
    }

    public static int differentMinByMillisecond(Date date1, Date date2) {
        return Math.abs((int)((date2.getTime() - date1.getTime()) / 60000L));
    }

    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    public static ArrayList<String> getLastDays(int intervals) {
        ArrayList<String> pastDaysList = new ArrayList();

        for(int i = intervals; i > 0; --i) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(6, calendar.get(6) - i);
            Date today = calendar.getTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            pastDaysList.add(format.format(today));
        }

        return pastDaysList;
    }
}